package com.wyp168.leetcode;

/**
 * @ClassName CustomSortStringSolution
 * @Title TODO
 * @Description TODO
 * @Author wyp168
 * @Date 2022/11/13 12:28
 */
public class CustomSortStringSolution {
    public static void main(String[] args) {
        String order = "cba";
        String s = "abcd";
        CustomSortStringSolution solution = new CustomSortStringSolution();
        System.out.println(solution.customSortString(order, s));
    }

    /**
     *
     * @param order 按照一定规律排序后的字符串
     * @param s 原始字符串，需要对照order字符串进行重新排序
     * @return
     */
    public String customSortString(String order, String s) {

        char[] chars = s.toCharArray();

        int fast = 0, slow = 0;

        for (int i = 0; i < order.length(); i ++) {
            char item = order.charAt(i);

            while (fast < chars.length) {
                if (chars[fast] == item) {
                    char temp = chars[slow];
                    chars[slow ++] = chars[fast];
                    chars[fast ++] = temp;
                    continue;
                }
                fast ++;
            }
            fast = slow;
        }

        return new String(chars);

    }
}
